package com.matchmaking.system.controller;

import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.matchmaking.system.config.PageBean;
import com.matchmaking.system.config.R;
import com.matchmaking.system.domain.SysDictType;
import com.matchmaking.system.domain.vo.SysDictTypeVo;
import com.matchmaking.system.service.SysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("dictType")
@CrossOrigin
public class SysDictTypeController {

    @Autowired
    SysDictTypeService dictTypeService;

    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 字典表分页查询
     * @return
     */
    @PostMapping("dictTypelist")
    public R dictTypelist(@RequestBody SysDictType dictType){
        PageHelper.startPage(dictType.getPageNum(),dictType.getPageSize());
        List<SysDictType> list = dictTypeService.selectAll(dictType);
        PageInfo<SysDictType> sysDictTypePageInfo = new PageInfo<>(list);
        return R.successed(sysDictTypePageInfo);
    }

    /**
     * 字典表添加
     * @param sysDictType
     * @return
     */
    @PostMapping("dictTypeAdd")
    public R dictTypeAdd(@RequestBody SysDictType sysDictType){
        //雪花算法
        long nextId = IdUtil.getSnowflake().nextId();
        sysDictType.setDictId(nextId);
        //redis击穿
        redisTemplate.opsForValue().set("type",sysDictType);
        dictTypeService.insertObject(sysDictType);
        return R.successed("添加成功");
    }


    /**
     * 逻辑删除
     * @param dictType
     * @return
     */
    @DeleteMapping("delById")
    public R delById(@RequestBody SysDictType dictType){
        dictTypeService.delById(dictType);
        dictType.setDelFlag("0");
        return R.successed("删除成功");
    }


    /**
     * 修改
     * @param dictType
     * @return
     */
    @PostMapping("updateById")
    public R updateById(@RequestBody SysDictType dictType){
        dictTypeService.updateObject(dictType);
        return R.successed("修改成功");
    }


    /**
     * 根据类型展示报表
     * @return
     */
    @GetMapping("dictTypeBingChart")
    public R dictTypeBingChart(){
        List<SysDictTypeVo> list = dictTypeService.getByBingChart();
        return R.successed(list);
    }


}
